using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using Quartz;
using Quartz.Examples;

namespace QuartzAdmin.web.Controllers
{
    public class TriggerController : Controller
    {
        Models.InstanceRepository instanceRepo = new QuartzAdmin.web.Models.InstanceRepository();

        //
        // GET: /Trigger/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Details(string instanceName, string groupName, string itemName)
        {
            Models.InstanceModel instance = instanceRepo.GetInstance(instanceName);
            Models.TriggerRepository trigRepo = new QuartzAdmin.web.Models.TriggerRepository(instance);

            Models.TriggerFireTimesModel m = new QuartzAdmin.web.Models.TriggerFireTimesModel();
            m.Trigger = trigRepo.GetTrigger(itemName, groupName);

            Models.CalendarRepository calRepo = new QuartzAdmin.web.Models.CalendarRepository(instance);
            m.Calendar = calRepo.GetCalendar(m.Trigger.CalendarName);
            m.Instance = instance;

            ViewData["groupName"] = groupName;

            if (m.Trigger == null)
            {
                ViewData["triggerName"] = itemName;
                return View("NotFound");
            }
            else
            {
                return View(m);
            }
        }

        public ActionResult FireTimes(string instanceName, string groupName, string itemName)
        {
            Models.InstanceModel instance = instanceRepo.GetInstance(instanceName);
            Models.TriggerRepository trigRepo = new QuartzAdmin.web.Models.TriggerRepository(instance);

            Models.TriggerFireTimesModel m = new QuartzAdmin.web.Models.TriggerFireTimesModel();
            m.Trigger = trigRepo.GetTrigger(itemName, groupName);

            Models.CalendarRepository calRepo = new QuartzAdmin.web.Models.CalendarRepository(instance);
            m.Calendar = calRepo.GetCalendar(m.Trigger.CalendarName);

            ViewData["groupName"] = groupName;

            if (m.Trigger == null)
            {
                ViewData["triggerName"] = itemName;
                return View("NotFound");
            }
            else
            {
                return View(m);
            }

        }

        public ActionResult Show()
        {
            Type jobType = typeof(IJob);
            var scheduler = AopContext.Default.Resolve<IScheduler>();
            Random rnd = new Random();
            var types = typeof(IExample).Assembly.GetTypes();
            foreach (var t in types)
            {
                if (jobType.IsAssignableFrom(t))
                {
                    var jd = new JobDetail(t.Name, t.Namespace, t, false, true, true);

                    scheduler.AddJob(jd, true);

                    if (scheduler.GetTrigger(t.Name, t.Namespace) == null)
                    {
                        Trigger trigger = new SimpleTrigger(t.Name, t.Namespace, DateTime.Now, null, 50000, TimeSpan.FromSeconds(rnd.Next(5, 10)));

                        trigger.JobName = t.Name;
                        trigger.JobGroup = t.Namespace;
                        scheduler.ScheduleJob(trigger);
                    }
                }
            }
            return View();
        }

    }
}
